package cn.hema.controller;

import cn.hema.mapper.RelationshipMapper;
import cn.hema.po.BorrowRecord;
import cn.hema.po.Message;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 商品的服务控制层
 */
@AllArgsConstructor
@RestController
@Slf4j
public class RelationshipController {

    private final RelationshipMapper RelationshipMapper;

    // 1. 根据ID获取书籍
    @PostMapping("/add")
    public Message addRE(@RequestBody BorrowRecord borrowRecord) {
        int before = RelationshipMapper.get();
        int have = RelationshipMapper.have(borrowRecord);
        Message message = new Message(borrowRecord);
        if (have > 0) {
            System.out.println("????");
            message.tag = "wa";
            return message;
        }
        RelationshipMapper.addRE(borrowRecord);
        System.out.println("in service : " + borrowRecord);
        int after = RelationshipMapper.get();

        if (before == after) {
            message.tag = "wa";
        } else {
            message.tag = "ac";
        }
        return message;
    }


    @PostMapping("/back")
    public Message back(@RequestBody BorrowRecord borrowRecord) {
        int have = RelationshipMapper.have(borrowRecord);
        Message message = new Message(borrowRecord);
        if (have == 0) {
            message.tag = "wa";
            return message;
        }
        borrowRecord.setReturnDate(LocalDateTime.now());
        RelationshipMapper.back(borrowRecord);
        message.tag = "ac";
        return message;
    }
}
